package com.tjmc.tiomsresource.service.impl;

import com.tjmc.tiomscore.dao.*;
import com.tjmc.tiomscore.model.*;
import com.tjmc.tiomsresource.parser.CMNelParser;
import com.tjmc.tiomsresource.service.CMNelService;
import com.tjmc.tiomsresource.service.CMSbnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Luc
 * @version 1.0
 * @date 2020-05-20 15:41
 */
@Service("cmNelService")
public class CMNelServiceImpl extends CMSuperServiceImpl<TranPtnHwNel> implements CMNelService {

    @Autowired
    private CMNelParser cmNelParser;
    @Autowired
    private TranPtnHwNelMapper tranPtnHwNelMapper;
    @Autowired
    private TranPtnNelMapper tranPtnNelMapper;
    @Autowired
    private TiNelMapper tiNelMapper;
    @Autowired
    private TiSubnetMapper tiSubnetMapper;
    @Autowired
    private CMSbnService cmSbnService;
    @Autowired
    private TranPtnSnnMapper tranPtnSnnMapper;

    @Override
    public CMNelParser getParser() {
        return cmNelParser;
    }

    @Override
    public TranPtnHwNelMapper getMapper() {
        return tranPtnHwNelMapper;
    }

    @Override
    public boolean saveTranPtnHwDataToDB(List<File> xmlFiles) {
        return this.saveTranPtnHwDataToDB(xmlFiles, "NEL");
    }

    @Override
    public void buildTiNelCoreToDB() {
        tiNelMapper.insertAllCoreNels();
    }

    @Override
    public void buildTiNelConvergenceToDB() {
        tiNelMapper.insertAllConvergenceNels();
//        tiNelMapper.correctConvergenceNelSubnetId();
        correctConvergenceNelSubnetId();
    }

    @Override
    public void buildTiNelAccessToDB() {
        tiNelMapper.insertAllAccessNels();
    }

    @Override
    public TiSubnet getTiSubnetByTiNelNativename(String nativename) {
        TiNelExample tiNelExample = new TiNelExample();
        tiNelExample.createCriteria().andNativenameEqualTo(nativename);
        List<TiNel> tiNelList = tiNelMapper.selectByExample(tiNelExample);
        if (null != tiNelList && tiNelList.size() > 0) {
            return tiSubnetMapper.selectByPrimaryKey(tiNelList.get(0).getSubnetId());
        }
        return null;
    }

    @Override
    public List<TiNel> getTiNelsByTiSubnet(TiSubnet tiSubnet, boolean onlyChildren) {
        if (null != tiSubnet) {
            if(!onlyChildren || (tiSubnet.getLayer() == 3 && !tiSubnet.getNativename().trim().endsWith("接入环"))) {
                TiNelExample tiNelExample = new TiNelExample();
                tiNelExample.createCriteria().andSubnetIdEqualTo(tiSubnet.getId());
                return tiNelMapper.selectByExample(tiNelExample);
            } else {
                List<TiSubnet> tiSubnetList = cmSbnService.getTiSubnetsByParentId(tiSubnet.getId());
                List<TiNel> tiNelList = new ArrayList<>();
                for (TiSubnet child : tiSubnetList) {
                    List<TiNel> tiNels = this.getTiNelsByTiSubnet(child, onlyChildren);
                    if(null != tiNels && tiNels.size() > 0) {
                        tiNelList.addAll(tiNels);
                    }
                }
                return tiNelList.size() > 0 ? tiNelList : null;
            }
        }
        return null;
    }

    @Override
    public TiNel getTiNelByTiNelNativename(String nativename) {
        TiNelExample tiNelExample = new TiNelExample();
        tiNelExample.createCriteria().andNativenameEqualTo(nativename);
        List<TiNel> tiNelList = tiNelMapper.selectByExample(tiNelExample);
        if(null != tiNelList && tiNelList.size() > 0) {
            return tiNelList.get(0);
        }
        return null;
    }

    @Override
    public List<TiSubnet> getTiSubnetsByTiNel(TiNel tiNel) {
        if(null != tiNel) {
            TranPtnSnnExample tranPtnSnnExample = new TranPtnSnnExample();
            tranPtnSnnExample.createCriteria().andRmuidEqualTo(tiNel.getRmuid());
            List<TranPtnSnn> tranPtnSnnList = tranPtnSnnMapper.selectByExample(tranPtnSnnExample);
            if(null != tranPtnSnnList && tranPtnSnnList.size() > 0) {
                List<TiSubnet> tiSubnetList = new ArrayList<>();
                for(TranPtnSnn tranPtnSnn : tranPtnSnnList) {
                    TiSubnetExample tiSubnetExample = new TiSubnetExample();
                    tiSubnetExample.createCriteria().andRmuidEqualTo(tranPtnSnn.getSubnetrmuid());
                    List<TiSubnet> tiSubnets =  tiSubnetMapper.selectByExample(tiSubnetExample);
                    if(null != tiSubnets && tiSubnets.size() > 0) {
                        tiSubnetList.addAll(tiSubnets);
                    }
                }
                return tiSubnetList.size()>0?tiSubnetList:null;
            }
        }
        return null;
    }

    @Override
    public void correctConvergenceNelSubnetId() {
        TiSubnetExample tiSubnetExample = new TiSubnetExample();
        tiSubnetExample.createCriteria().andNativenameEqualTo("核心汇聚层缩略图");
        List<TiSubnet> tiSubnetList = tiSubnetMapper.selectByExample(tiSubnetExample);
        if(null != tiSubnetList && tiSubnetList.size() > 0) {
            TiNelExample tiNelExample = new TiNelExample();
            tiNelExample.createCriteria().andSubnetIdEqualTo(tiSubnetList.get(0).getId());
            List<TiNel> tiNelList = tiNelMapper.selectByExample(tiNelExample);
            if(null != tiNelList && tiNelList.size() > 0) {
                for(TiNel tiNel : tiNelList) {
                    List<TiSubnet> tiSubnets = getTiSubnetsByTiNel(tiNel);
                    if(null != tiSubnets && tiSubnets.size() > 0) {
                        for(TiSubnet tiSubnet : tiSubnets) {
                            if(tiNel.getNativename().trim().startsWith(tiSubnet.getNativename().trim())) {
                                tiNel.setSubnetId(tiSubnet.getId());
                                tiNelMapper.updateByPrimaryKeySelective(tiNel);
                                break;
                            }
                        }
                    }
                }
            }
        }
    }

    @Override
    public void buildAllTiNelsToDB() {
        tiNelMapper.truncateTable();
        this.buildTiNelCoreToDB();
        this.buildTiNelConvergenceToDB();
        this.buildTiNelAccessToDB();
    }
}
